home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: C constant expression declarations
- Date: 15 Feb 1996 08:13:05 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4fvm2hINNa61@keats.ugrad.cs.ubc.ca>
- References: <31229735.41C67EA6@isi.com> <DMto56.Lo3@uns.bris.ac.uk>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <DMto56.Lo3@uns.bris.ac.uk>,
- Nathan Sidwell <nathan@pact.srf.ac.uk> wrote:
- >: ...and so on. Lately, I have been observing in code from other people
- >: equivalent declarations such as:
- >
- >: #define EXPR1 (1)
- >: #define EXPR2 (2)
- >
- >Whilst, as I'm sure you realise, it's necessary to put brackets round
- >things like (a + b), it is not necessary around unsigned constants.
- >However, a gotcha is using signed constants, for example
- >
- >#define EXPR -1
- >if(a EXPR) ...
-
- Are you sure that C has signed constants? That is, is -1 lexically recognized
- as a constant, or as a minus token followed by an integer quantity? It's
- probably done the latter way, which means that "-1" is a compound expression.
- If it weren't parsed this way, a construct like (a-1) would be parsed as
- something like '(' IDENTIFIER INTEGER ')', a syntax error. I'm not sure what th
- C standard says, just what common-sense about compiler construction says. :)
-
- >this will complile as 'a - 1', rather than give a syntax error
- >
- >leaving out the brackets, will not affect correct code, but can cause
- >incorrect code to be compiled to something undesired, rather than give
- >an error. Having brackets o unsigned constants, just reinforces the habit.
- >
- >Even wih the brackets, I could still have silent error, for instance
- >
- >#define EXPR (-1)
- >int a(int);
- >if(a EXPR) ...
- >
- >will be compiled as 'a(-1)'. However this compounds two errors, using
-
- But if you had #define EXPR (1), you still have the silent error. Thus this
- particular case does not hinge on whether or not you have a signed or
- unsigned constant.
-
- >a function name instead of a variable (I assume you meant to compare
- >a variable with a value), and omit the comparison operator.
-
- Parentheses in pre-processor macros unfortunately can't always protect you if
- you write an incorrect program, and it happens to parse as a valid sentence of
- the C language, especially if the pre-processor's expansion of your macros is
- not the cause of the problem.
- --
-
-